package com.black.web.app.store;


import com.black.app.domain.req.store.add.AppStoreProductsAddReq;
import com.black.app.domain.req.store.edit.AppStoreProductsEditReq;
import com.black.app.domain.req.store.query.AppStoreProductsQueryReq;
import com.black.app.domain.resp.store.AppStoreCategoryTreeResp;
import com.black.app.domain.resp.store.AppStoreCouponsResp;
import com.black.app.domain.resp.store.AppStoreProductsResp;
import com.black.app.domain.resp.store.AppStoreProductsTreeResp;
import com.black.app.service.store.AppStoreCategoryService;
import com.black.app.service.store.AppStoreProductSpecificationsService;
import com.black.app.service.store.AppStoreProductsService;
import com.black.common.annotation.Log;
import com.black.common.core.controller.BaseController;
import com.black.common.core.domain.R;
import com.black.common.enums.BusinessType;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "app-商品管理api")
@RestController
@RequestMapping(value = "/app/products")
public class AppStoreProductsController extends BaseController {

    @Autowired
    private AppStoreProductsService productsService;

    @Autowired
    private AppStoreCategoryService appStoreCategoryService;

    @Autowired
    private AppStoreProductSpecificationsService appStoreProductSpecificationsService;
    @ApiOperation(value = "获取商品分类树")
    @GetMapping(value = "/tree")
    public R<List<AppStoreCategoryTreeResp>> tree(){
        return R.ok(this.appStoreCategoryService.tree());
    }

    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/list")
    public R<PageInfo<AppStoreProductsResp>> list(AppStoreProductsQueryReq req){
        PageInfo<AppStoreProductsResp> page = this.productsService.paginationQuery(req);
        return R.ok(page);
    }

    @ApiOperation(value = "获取商品首页详情")
    @GetMapping(value =  "/home/data")
    public R<List<AppStoreProductsTreeResp>> homeStoreTree(){
        List<AppStoreProductsTreeResp> treeResp = this.appStoreCategoryService.homeStoreTree();
        return R.ok(treeResp);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public R<List<AppStoreProductsResp>> findById(@PathVariable(value = "id") Long id){
        List<AppStoreProductsResp> list = this.productsService.findById(id);
        return R.ok(list);
    }

    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加商品")
    @PostMapping
    public R add(@RequestBody AppStoreProductsAddReq req){
        this.productsService.add(req);
        return R.ok();
    }

    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除商品")
    @DeleteMapping("/{ids}")
    public R delete(@PathVariable(value = "ids") Long[] ids){
        this.productsService.deleteById(ids);
        return R.ok();
    }

    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改商品")
    @PutMapping
    public R update(@RequestBody @Valid AppStoreProductsEditReq req){
        this.productsService.edit(req);
        return R.ok();
    }



}
